/** ****************************************************************************
@defgroup COLOR_LIGHT ColorLight
@ingroup BSP_External
@brief 开关类(如LED/继电器) 驱动
@addtogroup ColorLight
@{
*******************************************************************************/

#ifndef _COLOR_LIGHT_H_
#define _COLOR_LIGHT_H_

#include "nonos.h"
#include "switch.h"

#ifdef __cplusplus
extern "C" {
#endif


/// RGB 彩灯驱动
typedef struct {
	FUNC_GPIO_PwmSetDuty funcRedPwmSetDuty;		///< GPIO 驱动函数: 红光修改 PWM 占空比
	FUNC_GPIO_PwmSetDuty funcGreenPwmSetDuty;	///< GPIO 驱动函数: 绿光修改 PWM 占空比
	FUNC_GPIO_PwmSetDuty funcBluePwmSetDuty;	///< GPIO 驱动函数: 蓝光修改 PWM 占空比
    uint16_t hue;									///< HSL: 色相
    uint16_t saturation;								///< HSL: 饱和度
	uint16_t lightness;								///< HSL: 亮度
    OSTaskId blinkTaskId;						///< 闪烁任务ID
    uint32_t blinkExOnTime;    						///< 闪烁亮的时间
} driver_color_light_t;

/**
 * @brief 初始化
 * @param dev 驱动数据结构指针
 * @param funcRedPwmSetDuty	  GPIO 驱动函数: 红光修改 PWM 占空比
 * @param funcGreenPwmSetDuty GPIO 驱动函数: 绿光修改 PWM 占空比
 * @param funcBluePwmSetDuty  GPIO 驱动函数: 蓝光修改 PWM 占空比
 */
void color_light_init(driver_color_light_t* dev, FUNC_GPIO_PwmSetDuty funcRedPwmSetDuty, FUNC_GPIO_PwmSetDuty funcGreenPwmSetDuty, FUNC_GPIO_PwmSetDuty funcBluePwmSetDuty);

/// @brief 取得当前色相
inline static uint16_t color_light_get_hue(driver_color_light_t* dev) { return dev->hue; }
/// @brief 设置色相
/// @return 状态是否变化了
bool color_light_set_hue(driver_color_light_t* dev, uint16_t hue);

/// @brief 取得当前饱和度
inline static uint16_t color_light_get_saturation(driver_color_light_t* dev) { return dev->saturation; }
/// @brief 设置饱和度
/// @return 状态是否变化了
bool color_light_set_saturation(driver_color_light_t* dev, uint16_t saturation);

/// @brief 取得当前亮度
inline static uint16_t color_light_get_lightness(driver_color_light_t* dev) { return dev->lightness; }
/// @brief 设置亮度
/// @return 状态是否变化了
bool color_light_set_lightness(driver_color_light_t* dev, uint16_t lightness);

/// @brief 设置 HSL 颜色
/// @return 状态是否变化了
bool color_light_set_hsl(driver_color_light_t* dev, uint16_t hue, uint16_t saturation, uint16_t lightness);
/// @breif 设置 RGB 颜色
/// @return 状态是否变化了
bool color_light_set_rgb(driver_color_light_t* dev, uint8_t red, uint8_t green, uint8_t blue);
/// @breif 设置色温
/// @return 状态是否变化了
bool color_light_set_temperature(driver_color_light_t* dev, uint16_t temperature);
/// @breif 设置色温和亮度
/// @return 状态是否变化了
bool color_light_set_ctl(driver_color_light_t* dev, uint16_t temperature, uint16_t lightness);

/// @brief TEMP_LIGHT 亮
/// @return 状态是否变化了
inline static bool color_light_on(driver_color_light_t* dev) { return color_light_set_lightness(dev, 100); }
/// @brief TEMP_LIGHT 灭
/// @return 状态是否变化了
inline static bool color_light_off(driver_color_light_t* dev) { return color_light_set_lightness(dev, 0); }
/// @brief TEMP_LIGHT 状态反转
inline static void color_light_toggle(driver_color_light_t* dev) { color_light_get_lightness(dev) ? color_light_off(dev) : color_light_on(dev); }

/// @brief 开关到指定状态
/// @param state: 0-OFF; 1-ON; 0xFF-REVERSE;
/// @return 状态是否变化了
bool color_light_switchTo(driver_color_light_t* dev, switch_state_t state, uint32_t keepTime1, uint32_t keepTime2, uint8_t flashTimes);

/// @brief 闪一下
/// @param time: 保持时间, ms
void color_light_blink(driver_color_light_t* dev, uint16_t time);

/// @brief 闪多下
/// @param onTime: 点亮时间, ms
/// @param offTime: 关闭时间, ms
/// @param count: 闪几次；=OSTASK_REPEAT_FOREVER 表示永远闪烁
/// @retval task, 可通过 osTaskKill() 来停止闪烁
OSTaskId color_light_blinkEx(driver_color_light_t* dev, uint16_t onTime, uint16_t offTime, uint16_t count);

/// @brief 停止闪烁
inline static void color_light_blinkStop(driver_color_light_t* dev) { osTaskDestroy(&dev->blinkTaskId); }


#ifdef __cplusplus
}
#endif

#endif  // _TEMP_LIGHT_H_
/// @}
